package ui;

import model.User;
import service.UserService;
import factory.ServiceFactory;
import util.InputValidator;

import java.util.Scanner;
import java.util.List;

public class RegisterUI { // 用户注册界面 完成人：姜凯严 7/3
    private UserService userService = ServiceFactory.getUserService();

    public void register() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("=== 用户注册 ===");
        
        // 使用InputValidator工具类进行用户名校验
        String username = InputValidator.getValidNonEmptyString(scanner, "请输入用户名: ", "用户名");
        if (username == null) return; // 校验失败
        
        // 检查用户名是否已存在
        List<User> exist = userService.findByUsername(username);
        if (exist != null && !exist.isEmpty()) {
            System.out.println("用户名已存在！");
            return;
        }
        
        // 使用InputValidator工具类进行密码校验
        String password = InputValidator.getValidNonEmptyString(scanner, "请输入密码: ", "密码");
        if (password == null) return; // 校验失败
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setBalance(0);
        user.setAddress("");
        user.setPhone("");
        user.setPermissionLevel(model.User.PermissionLevel.normal); // 注册默认普通用户
        if (userService.addUser(user)) {
            System.out.println("注册成功，请登录！");
        } else {
            System.out.println("注册失败！");
        }
    }
}
